package com.tangdada.whacmole;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.tangdada.whacmole.base.SoundPoolManager;
import com.tangdada.whacmole.base.Sprite;
import com.tangdada.whacmole.base.MediaPlayManager;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * 逻辑交互
 * Created by wjy on 2018/4/11.
 */

public class GameController {

    public static final int STATE_GAME_MENU = 1;
    public static final int STATE_GAME_HELP = 2;
    public static final int STATE_GAME_MAIN = 4;
    public static final int STATE_GAME_WIN = 5;
    public static final int STATE_GAME_OSE = 6;
    public static final int STATE_GAME_LOADING = 7;
    public static final int STATE_GAME_PAUSE = 8;

    private int mState = 0;//当前游戏状态
    private int mNewState = 0;//需要跳转的新界面
    private int mOldState = 0;//老界面

    private float mStartX, mStartY, mMoveX, mMoveY, mUpX, mUpY;
    private Paint mPaint;
    private Activity mContext;

    private MediaPlayManager mMediaPlayManager;
    private SoundPoolManager mSoundPoolManager;

    public GameController(Activity context) {
        this.mContext = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//消除锯齿
        mPaint.setStyle(Paint.Style.STROKE);//设置画笔风格
        mPaint.setAlpha(255);//画笔的不透明度
        mPaint.setStrokeWidth((float) 2);//设置笔触宽度
        mPaint.setColor(Color.WHITE);
        mMediaPlayManager = new MediaPlayManager(mContext);
        mSoundPoolManager = new SoundPoolManager(mContext);
        switchGameState(STATE_GAME_MENU, true, false);

    }

    private void paintGameState(Canvas canvas) {
        cleanScreen(canvas);
        switch (mState) {
            case STATE_GAME_LOADING:
//                drawGameLoading(canvas);
                break;
            case STATE_GAME_MENU:
                drawGameMenu(canvas);
                break;
            case STATE_GAME_MAIN:
                drawGameMain(canvas);
                break;
            default:
                cleanScreen(canvas);
                break;
        }
    }

    private void updateGameState() {
        switch (mState) {
            case STATE_GAME_LOADING:
//                updateGameLoading();
                break;
            case STATE_GAME_MENU:
                updateGameMenu();
                break;
            case STATE_GAME_MAIN:
                updateGameMain();
                break;
            default:
                break;
        }
    }

    private void releaseGameState() {
        switch (mState) {
            case STATE_GAME_LOADING:
//                releaseGameLoading();
                break;
            case STATE_GAME_MENU:
                releaseGameMenu();
                break;
            case STATE_GAME_MAIN:
                releaseGameMain();
                break;
            default:
                break;
        }
        System.gc();
    }

    private void initGameState() {
        switch (mState) {
            case STATE_GAME_LOADING:
//                initGameLoading();
                break;
            case STATE_GAME_MENU:
                initGameMenu();
                break;
            case STATE_GAME_MAIN:
                initGameMain();
                break;
            default:
                break;
        }

    }

    public void switchGameState(int newState, boolean isinit, boolean isrelease) {
        if (mState != newState) {
            mOldState = mState;
            if (isrelease) {
                releaseGameState();
            }
            mState = newState;
            if (isinit) {
                initGameState();
            }
        }
    }

    public void cleanScreen(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
    }


    //触摸点击事件
    public void onTouchDown(MotionEvent event) {
        mStartX = event.getX();
        mStartY = event.getY();
        mMoveX = mStartX;
        mMoveY = mStartY;
    }

    //触摸抬起事件
    public void onTouchUp(MotionEvent event) {
        mStartX = -1;
        mStartY = -1;
        mMoveX = -1;
        mMoveY = -1;
        mUpX = event.getX();
        mUpY = event.getY();
    }

    //触摸滑动事件
    public void onTouchMove(MotionEvent event) {
        mMoveX = event.getX();
        mMoveY = event.getY();
    }

    //返回键
    public void onKeyDown(int keyCode, KeyEvent e) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

        }
    }

    public void onDraw(Canvas c) {
        updateGameState();
        paintGameState(c);
    }


    /*****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
     * menu 游戏开始菜单
     ****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/

    public Bitmap menubg;
    public Sprite small_button_sprite;
    public Bitmap rule_bg;
    public Sprite start_sprite;
    public Sprite rule_sprite;
    public Sprite music_sprite;
    public Paint menu_paint;
    public Paint menu_small_paint;
    public Paint menu_small_red_paint;
    public boolean showRule;
    public int rule_bg_color;
    public StaticLayout text_layout;
    private final static String RULE = "游戏的规则很简单，每个洞随机出现一个地鼠抱着一个牌子，用手点击牌子KO一个美容问题，把冒出头来的不良牌子给打下去就算成功。KO数越高，分数越高，牌子有加分，也有减分，请看准了打击哦，每天可以玩2次。";

    private void initGameMenu() {
        menubg = ImageUtil.loadBitmap("menu_bg.png");
        rule_bg = ImageUtil.loadBitmap("rule_bg.png");
        start_sprite = new Sprite("menu_1.png", "menu_2.png", "menu_3.png");
        rule_sprite = new Sprite("menu_1.png", "menu_2.png");
        start_sprite.posX = (GameView.sGameWidth - start_sprite.width) / 2;
        start_sprite.posY = GameView.sGameHeight / 3 * 2 - 10 * GameView.sScale_Y;
        rule_sprite.posX = (GameView.sGameWidth - rule_sprite.width) / 2;
        rule_sprite.posY = GameView.sGameHeight / 3 * 2 + start_sprite.height + start_sprite.height / 4;
        music_sprite = new Sprite("music_1.png", "music_2.png");
        music_sprite.setPosition(GameView.sGameWidth - music_sprite.width - 8 * GameView.sScale_Y, 8 * GameView.sScale_Y);
        small_button_sprite = new Sprite("small_button_1.png", "small_button_2.png");
        small_button_sprite.setPosition((GameView.sGameWidth - small_button_sprite.width) / 2, GameView.sGameHeight / 4 + rule_bg.getHeight() - small_button_sprite.height / 2);
        menu_paint = new Paint();
        menu_paint.setTextAlign(Paint.Align.CENTER);
        menu_paint.setTextSize(start_sprite.height / 5 * 2);
        menu_paint.setColor(mContext.getResources().getColor(android.R.color.black));
        menu_paint.setTypeface(Typeface.DEFAULT_BOLD);
        menu_paint.setAntiAlias(true);
        menu_small_paint = new Paint(menu_paint);
        menu_small_paint.setTextSize(start_sprite.height / 3);
        menu_small_red_paint = new Paint(menu_small_paint);
        menu_small_red_paint.setColor(mContext.getResources().getColor(android.R.color.holo_red_light));
        music_sprite.setFrame(GameActivity.sMusicSwitch ? 0 : 1);
        rule_bg_color = Color.parseColor("#99000000");
        if (GameActivity.sMusicSwitch) mMediaPlayManager.start(R.raw.game_bg, true);
        TextPaint textPaint = new TextPaint(menu_small_paint);
        textPaint.setTextAlign(Paint.Align.LEFT);
        text_layout = new StaticLayout(RULE, textPaint, rule_bg.getWidth() * 3 / 4, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        showRule = false;
    }

    private void releaseGameMenu() {
        start_sprite.recycle();
        rule_sprite.recycle();
        menubg.recycle();
    }

    private void drawGameMenu(Canvas canvas) {

        canvas.drawBitmap(menubg, (GameView.sGameWidth - menubg.getWidth()) / 2, 0, mPaint);
        start_sprite.onDraw(canvas);
        music_sprite.onDraw(canvas);
        // 字体还不一样大小，这里偷懒了，蛋疼的UI
        canvas.drawText("开始游戏（", start_sprite.posX + start_sprite.width / 3 - menu_paint.getTextSize() / 4, start_sprite.posY + start_sprite.height / 2 + menu_paint.getTextSize() / 3, menu_paint);
        canvas.drawText("剩余 次", start_sprite.posX + menu_paint.measureText("开始游戏 （") + menu_small_paint.measureText("剩余 次") / 2 + 8 * GameView.sScale_X, start_sprite.posY + start_sprite.height / 2 + menu_small_paint.getTextSize() / 3, menu_small_paint);
        canvas.drawText("" + GameActivity.sPlayTimes, start_sprite.posX + +menu_paint.measureText("开始游戏（ 剩余") + 7 * GameView.sScale_X, start_sprite.posY + start_sprite.height / 2 + menu_small_paint.getTextSize() / 3, menu_small_red_paint);
        canvas.drawText("）", start_sprite.posX + +menu_paint.measureText("开始游戏（剩余  次"), start_sprite.posY + start_sprite.height / 2 + menu_paint.getTextSize() / 3, menu_paint);
        rule_sprite.onDraw(canvas);
        canvas.drawText("游戏规则", rule_sprite.posX + rule_sprite.width / 2, rule_sprite.posY + rule_sprite.height / 2 + menu_paint.getTextSize() / 3, menu_paint);
        if (showRule) drawRule(canvas);
    }

    private void drawRule(Canvas canvas) {
        canvas.drawColor(rule_bg_color);
        float left = (GameView.sGameWidth - rule_bg.getWidth()) / 2;
        float top = GameView.sGameHeight / 4;
        canvas.drawBitmap(rule_bg, left, top, mPaint);
        small_button_sprite.onDraw(canvas);
        canvas.translate((GameView.sGameWidth - rule_bg.getWidth()) / 2 + rule_bg.getWidth() / 8, top + rule_bg.getHeight() / 5);
        text_layout.draw(canvas);
        canvas.translate(-(GameView.sGameWidth - rule_bg.getWidth()) / 2 - rule_bg.getWidth() / 8, -top - rule_bg.getHeight() / 5);
        canvas.drawText("确认", small_button_sprite.posX + small_button_sprite.width / 2, small_button_sprite.posY + small_button_sprite.height * 7 / 11, menu_small_paint);
    }

    private void updateGameMenu() {
        if (showRule) {
            small_button_sprite.setFrame(small_button_sprite.isTouchMe(mMoveX, mMoveY) ? 1 : 0);
            if (small_button_sprite.isTouchMe(mUpX, mUpY)) {
                showRule = !showRule;
                mUpX = mUpY = -1;
            }
        } else {
            if (GameActivity.sPlayTimes <= 0) {
                start_sprite.setFrame(2);
            } else {
                start_sprite.setFrame(start_sprite.isTouchMe(mMoveX, mMoveY) ? 1 : 0);
            }
            rule_sprite.setFrame(rule_sprite.isTouchMe(mMoveX, mMoveY) ? 1 : 0);

            if (rule_sprite.isTouchMe(mUpX, mUpY)) {
                showRule = !showRule;
                mUpX = mUpY = -1;
            } else if (start_sprite.isTouchMe(mUpX, mUpY)) {
                if (GameActivity.sPlayTimes > 0) {
                    GameActivity.sPlayTimes--;
                    switchGameState(STATE_GAME_MAIN, true, true);
                }
                mUpX = mUpY = -1;
            } else if (music_sprite.isTouchMe(mUpX, mUpY)) {
                switchMusic();
            }
        }

    }
    // ///////////////////////////// menu ///////////////////////////////

    /*****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
     * main 游戏界面
     ****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/

    private final static int GAME_TIME = 1200;
    public Bitmap hole;
    public Bitmap hole_front;
    public int hole_pos[][];
    public final static int ROLE_NUMBER = 9;
    public Sprite weapon;
    public Mouse[] mouse;
    public Random random;
    public Bitmap game_bg;
    public Bitmap brand;
    public Bitmap boom;
    public Bitmap star;
    public Bitmap time_bg;
    public Bitmap time;
    public Bitmap time_bar;
    public Bitmap data_bg;
    public Sprite time_red;
    public Sprite pause_sprite;
    public Sprite small_button_1_sprite;
    public Sprite small_button_2_sprite;
    public float weaponPx, weaponPy;//武器和触摸点的偏差
    public boolean pause;
    public int game_time = GAME_TIME;
    private int score = 0;
    private int kick_right_num = 0;
    private int kick_wrong_num = 0;
    private int next_create_time;
    public Paint left_paint, right_paint;
    public Paint bad_paint, good_paint;
    public String good_tips[] = new String[]{"美妥", "超V", "点阵波", "疏糖饮"};
    public String bad_tips[] = new String[]{"肥胖", "皱纹", "色斑", "暗疮", "高血糖"};
    private final static int TIME_CHANGE[] = new int[]{55, 45, 20, 0};
    private final float SPACE_TIME[][] = new float[][]{
            {2f, 3f},
            {0.5f, 2f},
            {0.5f, 1.2f},
            {0.5f, 0.8f}
    };


    private void initGameMain() {
        random = new Random();
        game_bg = ImageUtil.loadBitmap("game_bg.png");
        hole = ImageUtil.loadBitmap("hole_behind.png");
        hole_front = ImageUtil.loadBitmap("hole_front.png");
        brand = ImageUtil.loadBitmap("brand.png");
        boom = ImageUtil.loadBitmap("boom.png");
        star = ImageUtil.loadBitmap("star.png");
        time_bg = ImageUtil.loadBitmap("time_bg.png");
        time = ImageUtil.loadBitmap("time.png");
        data_bg = ImageUtil.loadBitmap("data_bg.png");
        pause_sprite = new Sprite("pause_1.png", "pause_2.png");
        pause_sprite.setPosition(8 * GameView.sScale_X, 8 * GameView.sScale_X);
        time_red = new Sprite("time_red_1.png", "time_red_1.png", "time_red_1.png", "time_red_2.png", "time_red_2.png");
        time_bar = ImageUtil.loadBitmap("time_bar.png");
        weapon = new Sprite("hammer_1.png", "hammer_2.png", "hammer_3.png", "hammer_4.png", "hammer_4.png");
        hole_pos = new int[ROLE_NUMBER][2];
        mouse = new Mouse[9];
        for (int i = 0; i < ROLE_NUMBER; i++) {

            hole_pos[i][0] = (GameView.sGameWidth - hole_front.getWidth()) / 2 + ((i % 3) - 1) * hole_front.getWidth() / 4 * 3;
            hole_pos[i][1] = GameView.sGameHeight / 4 + (i / 3) * hole_front.getHeight() * 4 + ((i % 3) % 2) * hole_front.getHeight() * 2;
            if (i < mouse.length) {
                mouse[i] = new Mouse("mouse_1.png", "mouse_2.png", "mouse_3.png");
                mouse[i].posX = hole_pos[i][0] + hole_front.getWidth() / 2 - mouse[i].width / 2;
                mouse[i].initPosY = hole_pos[i][1] + hole_front.getHeight();
            }
        }
        small_button_1_sprite = new Sprite("small_button_1.png", "small_button_2.png");
        small_button_2_sprite = new Sprite("small_button_1.png", "small_button_2.png");
        small_button_1_sprite.setPosition(GameView.sGameWidth / 2 - small_button_1_sprite.width - 10 * GameView.sScale_X, GameView.sGameHeight / 3 + data_bg.getHeight() - small_button_1_sprite.height / 2);
        small_button_2_sprite.setPosition(GameView.sGameWidth / 2 + 10 * GameView.sScale_X, GameView.sGameHeight / 3 + data_bg.getHeight() - small_button_1_sprite.height / 2);
        time_red.setPosition((GameView.sGameWidth - time.getWidth()) / 2 - 8 * GameView.sScale_X, time_bar.getHeight() * 1.8f);
        time_red.playAnimation(999);
        weaponPx = weapon.width / 4f;
        weaponPy = weapon.height / 2 + weapon.height / 3;
        next_create_time = getNextCreateTime();
        mSoundPoolManager.addSound(0, R.raw.kick);
        mSoundPoolManager.addSound(1, R.raw.mouse);
        left_paint = new Paint(menu_paint);
        left_paint.setTextAlign(Paint.Align.LEFT);
        right_paint = new Paint(menu_paint);
        right_paint.setTextAlign(Paint.Align.RIGHT);
        bad_paint = new Paint(menu_small_paint);
        bad_paint.setTextSize(menu_small_paint.getTextSize() * 2 / 3);
        bad_paint.setColor(Color.parseColor("#F71C5F"));
        good_paint = new Paint(bad_paint);
        good_paint.setColor(Color.parseColor("#0261FF"));
        reset();
    }

    private void reset() {
        pause = false;
        score = 0;
        kick_wrong_num = 0;
        kick_right_num = 0;
        game_time = GAME_TIME;
        for (Mouse m : mouse) {
            m.reset();
        }
    }

    private void createMouseByRandom() {
        List<Mouse> list = Arrays.asList(mouse);
        Collections.shuffle(list);
        for (Mouse mouse : list) {
            if (mouse.state == Mouse.STATE_NONE) {
                String tip = "";
                boolean good = false;
                if (random.nextInt(10) > 7) {
                    tip = good_tips[random.nextInt(good_tips.length)];
                    good = true;
                } else {
                    tip = bad_tips[random.nextInt(bad_tips.length)];
                    good = false;
                }
                mouse.create(false, tip, good);
                break;
            }
        }
    }

    private int getNextCreateTime() {
        int index = getTimePhase();
        int randomTime = random.nextInt((int) ((SPACE_TIME[index][1] - SPACE_TIME[index][0]) * 20));
        int createTime = (int) (SPACE_TIME[index][0] * 20) + randomTime;
        return createTime;
    }

    private int getTimePhase() {
        int index = 0;
        for (int time : TIME_CHANGE) {
            if (game_time > time * 20) return index;
            index++;
        }
        return 0;
    }

    private void releaseGameMain() {
        game_bg.recycle();
        hole.recycle();
        hole_front.recycle();
        brand.recycle();
        boom.recycle();
        star.recycle();
        time_bg.recycle();
        time.recycle();
        data_bg.recycle();
        pause_sprite.recycle();
        time_red.recycle();
        time_bar.recycle();
        weapon.recycle();
        if (mouse != null) {
            for (Mouse m : mouse) {
                m.recycle();
            }
            mouse = null;
        }
        mSoundPoolManager.release();
    }

    private void drawGameMain(Canvas canvas) {
        canvas.drawBitmap(game_bg, (GameView.sGameWidth - menubg.getWidth()) / 2, 0 - 60 * GameView.sScale_Y, mPaint);
        drawGameTimeBar(canvas);

        for (int[] h : hole_pos) {
            canvas.drawBitmap(hole, h[0] + (hole_front.getWidth() - hole.getWidth()) / 2, h[1] - hole.getHeight() / 4, mPaint);
        }

        for (Mouse m : mouse) {
            canvas.save();
            canvas.clipRect(m.posX - m.width / 3, m.initPosY - m.height * 2, m.posX + m.width * 2, m.initPosY - m.height / 6);
            m.onDraw(canvas);
            if (m.state == Mouse.STATE_HIT) {
                canvas.drawBitmap(boom, m.posX - boom.getWidth() / 5, m.posY - boom.getHeight() / 4, mPaint);
            } else if (m.state == Mouse.STATE_YUN) {
                canvas.drawBitmap(star, m.posX + (m.width - star.getWidth()) / 2, m.posY - star.getHeight() / 2, mPaint);
            }
            canvas.restore();
        }
        for (int[] h : hole_pos) {
            canvas.drawBitmap(hole_front, h[0], h[1], mPaint);
        }
        for (Mouse m : mouse) {
            if (m.state == Mouse.STATE_WAIT || m.state == Mouse.STATE_HIT) {
                canvas.drawBitmap(brand, m.posX + m.width * 2 / 3 + m.width / 12, m.posY - m.height / 4, mPaint);
                float tx = m.posX + m.width + m.width / 5;
                float ty = m.posY + m.height / 3.5f;
                canvas.rotate(35, tx, ty);
                canvas.drawText(m.tip, tx, ty, m.isGood ? good_paint : bad_paint);
                canvas.rotate(-35, tx, ty);
            }
        }
        if (weapon.anim_times > 0) weapon.onDraw(canvas);
        if (pause) drawGamePause(canvas);
    }

    private void drawGameTimeBar(Canvas canvas) {
        canvas.drawBitmap(time_bg, 0 - 8 * GameView.sScale_X, 0, mPaint);
        canvas.drawBitmap(time_bar, (GameView.sGameWidth - time_bar.getWidth()) / 2, time_bar.getHeight() * 2, mPaint);
        canvas.save();
        canvas.clipRect((GameView.sGameWidth - time.getWidth()) / 2 - 10 * GameView.sScale_X, time_bar.getHeight() * 1.5f, (GameView.sGameWidth - time.getWidth()) / 2 + time.getWidth() * game_time / GAME_TIME, time_bar.getHeight() * 1.5f + time.getHeight() * 2);
        if (game_time < (GAME_TIME / 6)) {
            if (game_time > 0) time_red.onDraw(canvas);
        } else {
            canvas.drawBitmap(time, (GameView.sGameWidth - time.getWidth()) / 2, time_bar.getHeight() * 2 + 2.5f * GameView.sScale_Y, mPaint);
        }
        canvas.restore();
        music_sprite.onDraw(canvas);
        pause_sprite.onDraw(canvas);

        canvas.drawText("得分：" + score + "分", GameView.sGameWidth / 2, time_red.posY - time.getHeight() / 2 + 3 * GameView.sScale_Y, menu_small_paint);
    }

    private void drawGamePause(Canvas canvas) {
        float left = (GameView.sGameWidth - data_bg.getWidth()) / 2;
        float top = GameView.sGameHeight / 3;
        canvas.drawBitmap(data_bg, left, top, mPaint);
        top = GameView.sGameHeight / 3 + left_paint.getTextSize();
        canvas.drawText("击打数", left + small_button_1_sprite.width / 4, top + small_button_1_sprite.height, left_paint);
        canvas.drawText((kick_right_num + kick_wrong_num) + "次", left + data_bg.getWidth() - small_button_1_sprite.width / 4, top + small_button_1_sprite.height, right_paint);
        canvas.drawText("击打正确率", left + small_button_1_sprite.width / 4, top + small_button_1_sprite.height * 2, left_paint);
        canvas.drawText(((kick_right_num + kick_wrong_num == 0) ? "0" : (kick_right_num * 100 / (kick_right_num + kick_wrong_num))) + "%", left + data_bg.getWidth() - small_button_1_sprite.width / 4, top + small_button_1_sprite.height * 2, right_paint);
        canvas.drawText("本次游戏得分", left + small_button_1_sprite.width / 4, top + small_button_1_sprite.height * 3, left_paint);
        canvas.drawText(score + "分", left + data_bg.getWidth() - small_button_1_sprite.width / 4, top + small_button_1_sprite.height * 3, right_paint);
        small_button_1_sprite.onDraw(canvas);
        small_button_2_sprite.onDraw(canvas);
        canvas.drawText(game_time <= 0 ? "再玩一次" : "继续游戏", small_button_1_sprite.posX + small_button_1_sprite.width / 2, small_button_1_sprite.posY + small_button_1_sprite.height * 7 / 11, menu_small_paint);
        canvas.drawText("退出游戏", small_button_2_sprite.posX + small_button_1_sprite.width / 2, small_button_2_sprite.posY + small_button_1_sprite.height * 7 / 11, menu_small_paint);
    }

    private void updateGameMain() {
        if (pause) {
            small_button_1_sprite.setFrame(small_button_1_sprite.isTouchMe(mMoveX, mMoveY) ? 1 : 0);
            small_button_2_sprite.setFrame(small_button_2_sprite.isTouchMe(mMoveX, mMoveY) ? 1 : 0);
            if (small_button_1_sprite.isTouchMe(mUpX, mUpY)) {
                pause = !pause;
                if (game_time <= 0) {
                    reset();
                }
                mUpX = mUpY = -1;
            } else if (small_button_2_sprite.isTouchMe(mUpX, mUpY)) {
                mUpX = mUpY = -1;

                switchGameState(STATE_GAME_MENU, true, true);
            }
        } else {
            pause_sprite.setFrame(pause_sprite.isTouchMe(mUpX, mUpY) ? 1 : 0);
            if (pause_sprite.isTouchMe(mUpX, mUpY)) {
                pause = !pause;
                mUpX = mUpY = -1;
            } else if (music_sprite.isTouchMe(mUpX, mUpY)) {
                switchMusic();
            } else if (mMoveX > 0 && mMoveY > time_bg.getHeight()) {
                if (weapon.anim_times < 1) {
                    weapon.setPosition(mMoveX - weaponPx, mMoveY - weaponPy);
                    weapon.playAnimation(1);
                }
            }
            if (weapon.getAnimFrame() == 3) {
                if (GameActivity.sMusicSwitch) mSoundPoolManager.playSound(0);
            }

            //检测被打老鼠
            for (Mouse m : mouse) {
                m.onUpdate();
                if (m.state == Mouse.STATE_WAIT && m.isTouchMe(weapon.posX + weaponPx, weapon.posY + weaponPy) && weapon.getAnimFrame() == 3) {
                    m.state = Mouse.STATE_HIT;
                    m.wait_time = 5;
                    if (m.isGood) {
                        score = --score < 0 ? 0 : score;
                        kick_wrong_num++;
                    } else {
                        score++;
                        kick_right_num++;
                    }
                    if (GameActivity.sMusicSwitch) mSoundPoolManager.playSound(1);
                    break;
                }
            }
            game_time--;
            if (game_time <= 0) {
                pause = true;
            }
            next_create_time--;
            if (next_create_time <= 0) {
                createMouseByRandom();
                next_create_time = getNextCreateTime();
            }
        }
    }
    // ///////////////////////////// main ///////////////////////////////

    private void switchMusic() {
        GameActivity.sMusicSwitch = !GameActivity.sMusicSwitch;
        mContext.getSharedPreferences("game", mContext.MODE_PRIVATE).edit().putBoolean("music", GameActivity.sMusicSwitch).commit();
        music_sprite.setFrame(GameActivity.sMusicSwitch ? 0 : 1);
        if (GameActivity.sMusicSwitch) {
            mMediaPlayManager.start(R.raw.game_bg, true);
        } else {
            mMediaPlayManager.stop();
        }
        mUpX = mUpY = -1;
        mMoveX = mMoveY = -1;
    }

    public void onStop() {
        mMediaPlayManager.stop();
    }

    public void onResume() {
        if (GameActivity.sMusicSwitch && mMediaPlayManager != null) mMediaPlayManager.restart();
    }
}
